import BASE_CONFIG from './config';
import { Toast, loading } from './function';
import { FILEURL } from './constant';
export const upload = (filePath: string) => {
	return new Promise<Array<any> | object | string>((resolve, reject) => {
		uni.uploadFile({
			url: BASE_CONFIG.BASE_URL+FILEURL,
			filePath: filePath,
			name: 'file',
			success: (uploadFileRes) => {
				let data = JSON.parse(uploadFileRes.data);
				if (!data.code) {
					resolve(data.data);
				} else {
					Toast('上传失败');
				}
			},
			fail: (error) => {
				reject(error);
			},
		});
	});
};
/**
 * @description 单选择图片上传 返回图片地址
 */
export const upLoadChooseImage = (count?: number) => {
	return new Promise<Array<string>>((resolve, reject) => {
		uni.chooseImage({
			count: count || 1,
			sizeType: ['original', 'compressed'],
			success: async (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				const length = tempFilePaths.length;
				let tempFileArr: Array<string> = [];
				for (const iterator of tempFilePaths) {
					let res = await upload(iterator as string);
					tempFileArr.push(res as string);
					if (length === tempFileArr.length) {
						resolve(tempFileArr);
					}
				}
			},
			fail: (error) => {
				reject(error);
			}
		})
	})
}
